<template>
  <div class="login-wrap">
    <el-form class="login-form"
             :label-position="labelPosition"
             label-width="80px"
             :model="formdata">
      <h2>用户登录</h2>
      <el-form-item label="用户名">
        <el-input v-model="formdata.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="formdata.password"></el-input>
      </el-form-item>
      <el-button class="login-btn"
                 @click.prevent="handleLogin()"
                 type="primary">登录</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formdata: {
        username: 'admin',
        password: '123',
      },
      labelPosition: 'top',
    };
  },
  methods: {
    // 登录请求
    async handleLogin() {
      const res = await this.$http.post('login', this.formdata);
      if (res.data.username == 'admin' && res.data.password == '123') {
        // 保存token
        localStorage.setItem('token', res.data.token);
        console.log(res);
        this.$router.push({ name: 'home1' });
        this.$message.success('登录成功！');
      } else {
        this.$message.errror('登录失败！');
      }
    },
  },
};
</script>

<style scoped>
.login-wrap {
  height: 100%;
  background-color: #324152;
  /* 弹性盒布局 */
  display: flex;
  justify-content: center;
  align-items: center;
}
.login-wrap .login-form {
  width: 400px;
  background-color: #fff;
  border-radius: 5px;
  padding: 30px;
}

.login-wrap .login-btn {
  width: 100%;
}
</style>